package com.spic.business.violation.supervision.speed.service.impl;

import com.google.common.collect.Lists;
import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.violation.supervision.speed.domain.VehicleSpeedRecordDTO;
import com.spic.business.violation.supervision.speed.domain.ViolationsSpeedRecord;
import com.spic.business.violation.supervision.speed.domain.ViolationsSpeedRules;
import com.spic.business.violation.supervision.speed.mapper.ViolationsSpeedRulesMapper;
import com.spic.business.violation.supervision.speed.service.IViolationsSpeedRulesService;
import com.spic.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 超速规则Service业务层处理
 *
 * @author spic
 * @date 2021-10-13
 */
@Service
public class ViolationsSpeedRulesServiceImpl implements IViolationsSpeedRulesService
{
    @Autowired
    private ViolationsSpeedRulesMapper violationsSpeedRulesMapper;

    /**
     * 查询超速规则
     *
     * @param id 超速规则主键
     * @return 超速规则
     */
    @Override
    public ViolationsSpeedRules selectViolationsSpeedRulesById(Long id)
    {
        return violationsSpeedRulesMapper.selectViolationsSpeedRulesById(id);
    }

    /**
     * 查询超速规则列表
     *
     * @param violationsSpeedRules 超速规则
     * @return 超速规则
     */
    @Override
    @BusinessDataScope(menuPath = "business/violation/speed_rules/index")
    public List<ViolationsSpeedRules> selectViolationsSpeedRulesList(ViolationsSpeedRules violationsSpeedRules)
    {
        return violationsSpeedRulesMapper.selectViolationsSpeedRulesList(violationsSpeedRules);
    }

    /**
     * 新增超速规则
     *
     * @param violationsSpeedRules 超速规则
     * @return 结果
     */
    @Override
    public int insertViolationsSpeedRules(ViolationsSpeedRules violationsSpeedRules)
    {
        violationsSpeedRules.setCreateTime(DateUtils.getNowDate());
        return violationsSpeedRulesMapper.insertViolationsSpeedRules(violationsSpeedRules);
    }

    /**
     * 修改超速规则
     *
     * @param violationsSpeedRules 超速规则
     * @return 结果
     */
    @Override
    public int updateViolationsSpeedRules(ViolationsSpeedRules violationsSpeedRules)
    {
        violationsSpeedRules.setUpdateTime(DateUtils.getNowDate());
        return violationsSpeedRulesMapper.updateViolationsSpeedRules(violationsSpeedRules);
    }

    /**
     * 批量删除超速规则
     *
     * @param ids 需要删除的超速规则主键
     * @return 结果
     */
    @Override
    public int deleteViolationsSpeedRulesByIds(Long[] ids)
    {
        return violationsSpeedRulesMapper.deleteViolationsSpeedRulesByIds(ids);
    }

    /**
     * 删除超速规则信息
     *
     * @param id 超速规则主键
     * @return 结果
     */
    @Override
    public int deleteViolationsSpeedRulesById(Long id)
    {
        return violationsSpeedRulesMapper.deleteViolationsSpeedRulesById(id);
    }

    @Override
    public List<ViolationsSpeedRecord> getOverSpeedRecords(List<VehicleSpeedRecordDTO> vehicleSpeedRecordDTOs) {
        if(CollectionUtils.isEmpty(vehicleSpeedRecordDTOs)) return Collections.emptyList();
        List<ViolationsSpeedRules> rules= violationsSpeedRulesMapper.selectViolationsSpeedRulesList(new ViolationsSpeedRules());
        Map<String, List<ViolationsSpeedRules>> ruleMap = rules.stream().collect(Collectors.groupingBy(ViolationsSpeedRules::getArea));
        List<ViolationsSpeedRecord> records = Lists.newArrayList();
        for(VehicleSpeedRecordDTO dto: vehicleSpeedRecordDTOs){
            List<ViolationsSpeedRules> subRules = ruleMap.get(dto.getIllegalArea());
            if(subRules == null) continue;
            boolean isOverSpeed = subRules.stream().filter(r->dto.getVehicleType().equals(r.getVehicleType()))
                    .anyMatch(r->r.getMaxSpeed()<dto.getRunningSpeed());
            if(isOverSpeed){records.add(dto.toRecord());}
        }
        return records;
    }
}
